<template>
  <div class="message-management">
    <!-- 搜索栏 -->
    <el-card class="search-card">
      <el-form :inline="true" :model="searchForm">
        <el-form-item label="消息类型">
          <el-select v-model="searchForm.type" placeholder="请选择消息类型">
            <el-option label="全部" value="" />
            <el-option label="系统通知" value="system" />
            <el-option label="活动通知" value="activity" />
            <el-option label="审核通知" value="review" />
          </el-select>
        </el-form-item>
        <el-form-item label="发送状态">
          <el-select v-model="searchForm.status" placeholder="请选择状态">
            <el-option label="全部" value="" />
            <el-option label="未发送" value="pending" />
            <el-option label="已发送" value="sent" />
            <el-option label="发送失败" value="failed" />
          </el-select>
        </el-form-item>
        <el-form-item label="时间范围">
          <el-date-picker
            v-model="searchForm.dateRange"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">搜索</el-button>
          <el-button @click="resetSearch">重置</el-button>
          <el-button type="success" @click="handleAdd">新增消息</el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 消息列表 -->
    <el-card class="table-card">
      <el-table :data="messageList" border stripe>
        <el-table-column type="selection" width="55" />
        <el-table-column prop="title" label="消息标题" />
        <el-table-column prop="type" label="消息类型" width="120">
          <template #default="{ row }">
            <el-tag :type="getTypeTag(row.type)">{{ getTypeText(row.type) }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="recipients" label="接收对象" width="120" />
        <el-table-column prop="sendTime" label="发送时间" width="180" />
        <el-table-column prop="status" label="状态" width="100">
          <template #default="{ row }">
            <el-tag :type="getStatusType(row.status)">
              {{ getStatusText(row.status) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="sender" label="发送人" width="120" />
        <el-table-column label="操作" width="200" fixed="right">
          <template #default="{ row }">
            <el-button 
              v-if="row.status === 'pending'"
              link 
              type="primary" 
              @click="handleSend(row)"
            >发送</el-button>
            <el-button link type="primary" @click="handleEdit(row)">编辑</el-button>
            <el-button link type="primary" @click="handlePreview(row)">预览</el-button>
            <el-button link type="danger" @click="handleDelete(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :total="total"
          :page-sizes="[10, 20, 50, 100]"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>

    <!-- 消息表单对话框 -->
    <el-dialog
      v-model="dialogVisible"
      :title="dialogType === 'add' ? '新增消息' : '编辑消息'"
      width="700px"
    >
      <el-form
        ref="messageFormRef"
        :model="messageForm"
        :rules="messageRules"
        label-width="100px"
      >
        <el-form-item label="消息标题" prop="title">
          <el-input v-model="messageForm.title" placeholder="请输入消息标题" />
        </el-form-item>
        <el-form-item label="消息类型" prop="type">
          <el-select v-model="messageForm.type" placeholder="请选择消息类型">
            <el-option label="系统通知" value="system" />
            <el-option label="活动通知" value="activity" />
            <el-option label="审核通知" value="review" />
          </el-select>
        </el-form-item>
        <el-form-item label="接收对象" prop="recipients">
          <el-select
            v-model="messageForm.recipients"
            multiple
            placeholder="请选择接收对象"
          >
            <el-option label="全部用户" value="all" />
            <el-option label="志愿者" value="volunteers" />
            <el-option label="组织" value="organizations" />
          </el-select>
        </el-form-item>
        <el-form-item label="消息内容" prop="content">
          <el-input
            v-model="messageForm.content"
            type="textarea"
            :rows="6"
            placeholder="请输入消息内容"
          />
        </el-form-item>
        <el-form-item label="定时发送">
          <el-switch v-model="messageForm.isScheduled" />
        </el-form-item>
        <el-form-item label="发送时间" v-if="messageForm.isScheduled">
          <el-date-picker
            v-model="messageForm.scheduledTime"
            type="datetime"
            placeholder="选择发送时间"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleSubmit">确定</el-button>
        </span>
      </template>
    </el-dialog>

    <!-- 预览对话框 -->
    <el-dialog
      v-model="previewDialogVisible"
      title="消息预览"
      width="500px"
    >
      <div class="message-preview">
        <h3>{{ currentMessage.title }}</h3>
        <div class="message-meta">
          <span>类型：{{ getTypeText(currentMessage.type) }}</span>
          <span>发送时间：{{ currentMessage.sendTime }}</span>
        </div>
        <div class="message-content">
          {{ currentMessage.content }}
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'

// 搜索表单
const searchForm = reactive({
  type: '',
  status: '',
  dateRange: []
})

// 分页数据
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(100)

// 消息列表
const messageList = ref([
  {
    id: 1,
    title: '系统维护通知',
    type: 'system',
    recipients: '全部用户',
    sendTime: '2024-03-20 10:00',
    status: 'sent',
    sender: 'admin',
    content: '系统将于今晚22:00-23:00进行例行维护...'
  },
  // ... 更多数据
])

// 对话框控制
const dialogVisible = ref(false)
const dialogType = ref('add')
const previewDialogVisible = ref(false)
const currentMessage = ref({})

// 消息表单
const messageForm = reactive({
  title: '',
  type: '',
  recipients: [],
  content: '',
  isScheduled: false,
  scheduledTime: ''
})

// 表单校验规则
const messageRules = {
  title: [
    { required: true, message: '请输入消息标题', trigger: 'blur' }
  ],
  type: [
    { required: true, message: '请选择消息类型', trigger: 'change' }
  ],
  recipients: [
    { required: true, message: '请选择接收对象', trigger: 'change' }
  ],
  content: [
    { required: true, message: '请输入消息内容', trigger: 'blur' }
  ]
}

// 获取消息类型标签
const getTypeTag = (type) => {
  const tags = {
    system: 'info',
    activity: 'success',
    review: 'warning'
  }
  return tags[type] || 'info'
}

// 获取消息类型文本
const getTypeText = (type) => {
  const texts = {
    system: '系统通知',
    activity: '活动通知',
    review: '审核通知'
  }
  return texts[type] || '未知'
}

// 获取状态类型
const getStatusType = (status) => {
  const types = {
    pending: 'warning',
    sent: 'success',
    failed: 'danger'
  }
  return types[status] || 'info'
}

// 获取状态文本
const getStatusText = (status) => {
  const texts = {
    pending: '未发送',
    sent: '已发送',
    failed: '发送失败'
  }
  return texts[status] || '未知'
}

// 处理搜索
const handleSearch = () => {
  // 实现搜索逻辑
}

// 重置搜索
const resetSearch = () => {
  Object.keys(searchForm).forEach(key => {
    searchForm[key] = key === 'dateRange' ? [] : ''
  })
}

// 处理新增
const handleAdd = () => {
  dialogType.value = 'add'
  dialogVisible.value = true
  Object.keys(messageForm).forEach(key => {
    messageForm[key] = key === 'recipients' ? [] : key === 'isScheduled' ? false : ''
  })
}

// 处理编辑
const handleEdit = (row) => {
  dialogType.value = 'edit'
  dialogVisible.value = true
  Object.keys(messageForm).forEach(key => {
    messageForm[key] = row[key]
  })
}

// 处理预览
const handlePreview = (row) => {
  currentMessage.value = { ...row }
  previewDialogVisible.value = true
}

// 处理发送
const handleSend = (row) => {
  ElMessageBox.confirm(
    '确认发送该消息？',
    '提示',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }
  ).then(() => {
    ElMessage.success('发送成功')
  })
}

// 处理删除
const handleDelete = (row) => {
  ElMessageBox.confirm(
    '确认删除该消息？',
    '警告',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    }
  ).then(() => {
    ElMessage.success('删除成功')
  })
}

// 处理表单提交
const handleSubmit = () => {
  ElMessage.success(dialogType.value === 'add' ? '添加成功' : '更新成功')
  dialogVisible.value = false
}

// 处理分页
const handleSizeChange = (val) => {
  pageSize.value = val
  // 重新加载数据
}

const handleCurrentChange = (val) => {
  currentPage.value = val
  // 重新加载数据
}
</script>

<style scoped lang="scss">
.message-management {
  .search-card {
    margin-bottom: 20px;
  }

  .table-card {
    .pagination {
      margin-top: 20px;
      display: flex;
      justify-content: flex-end;
    }
  }

  .message-preview {
    padding: 20px;

    h3 {
      margin: 0 0 16px;
      font-size: 18px;
    }

    .message-meta {
      margin-bottom: 16px;
      color: #909399;
      font-size: 14px;

      span {
        margin-right: 20px;
      }
    }

    .message-content {
      line-height: 1.6;
      white-space: pre-wrap;
    }
  }
}
</style> 